package web.demo.service.impl;

import java.io.File;
import java.util.List;

import web.demo.dao.impl.FoodDao;
import web.demo.entity.Food;
import web.demo.factory.BeanFactory;
import web.demo.service.IFoodService;
import web.demo.utils.PageBean;

public class FoodService implements IFoodService{
	
	private FoodDao foodDao=BeanFactory.getInstance("foodDao",FoodDao.class);
	
	@Override
	public boolean addFood(Food food) {
		return foodDao.addFood(food);
	}

	@Override
	public void update(Food food) {
		foodDao.update(food);
	}

	@Override
	public boolean delete(int id) {
		//先删除该食品的图片
		return foodDao.delete(id);
	}

	@Override
	public List<Food> getFoodAll(PageBean pageBean) {
		return foodDao.getFoodAll(pageBean);
	}

	@Override
	public List<Object[]> getAll(PageBean pageBean) {
		return foodDao.getAll(pageBean);
	}

	@Override
	public int getCount(PageBean pageBean) {
		// TODO Auto-generated method stub
		return foodDao.getCount(pageBean);
	}

	@Override
	public int getFoodCount(PageBean pageBean) {
		// TODO Auto-generated method stub
		return foodDao.getFoodCount(pageBean);
	}

	@Override
	public Food getFoodById(int id) {
		return foodDao.getFoodById(id);
	}

	@Override
	public boolean delete(int id, String appPath,String sysPath) {
		//拼接图片路径
		String path=getFoodById(id).getImg();
		String file1Path=appPath+"\\"+path;
		String file2Path=sysPath+"\\"+path;
		File file1=new File(file1Path);
		File file2=new File(file2Path);
		boolean rs=true;
		if(file1.exists()){
			rs=file1.delete();
		}
		if(file2.exists()){
			rs=file2.delete();
		}
		if(rs){
			return foodDao.delete(id);
		}
		return false;
	}

	@Override
	public List<Food> getAll() {
		return foodDao.getAll();
	}

	@Override
	public List<Food> getByFoodType(int foodTypeId) {
		return foodDao.getByFoodType(foodTypeId);
	}

}
